<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsMultiLanguage extends BgcmsProcess {
	var $db;
	var $smarty;
	
    public function __construct($db, $smarty) {
    	$this->db = $db;
    	$this->smarty = $smarty;
    }
    
    function process() 
	{
		if($_SESSION['user_isadmin'] == '1')
		{
			if(isset($_GET['sub']) && $_GET['sub']== 'translator'){
				$this->translator();	
			}else if(isset($_GET['sub']) && $_GET['sub']== 'lang'){
				$this->language();
			}else{
				$this->setting_language();	
			}	
		}
		else
			$this->translator();			
			
		$this->Run();
		
    }
    
	function setting_language(){
		if(isset($_POST['act']) && $_POST['act']== 'settinglang'){
			$lang_active = 0;
			if(isset($_POST['act']) && $_POST['lang_active']!= ''){
				$lang_active = 1;
			}
			BgcmsMultiLanguageBean::update_active_lang($this->db,"1",$lang_active,$_POST['choose']);
			header("Location: ./?module=BgcmsMultiLanguage");	
		}
		$lang = BgcmsMultiLanguageBean::get_all_lang($this->db);
		$setting = BgcmsMultiLanguageBean::get_active_lang($this->db,'1');
		$this->smarty->assign("ALL_LANGUAGE",$lang);
		$this->smarty->assign("SETTINGLANG",$setting);
	}
	function language() 
	{
	
		if(isset($_POST['act']) && $_POST['act']== 'addlang')
		{
			$this->create_language();	
		}
		else if(isset($_POST['act']) && $_POST['act']== 'updatelanguage')
		{
			$this->update_language();	
		}	
		else if(isset($_POST['act']) && $_POST['act']== 'dellang')
		{
			$getkey = BgcmsMultiLanguageBean::get_lang_bykey($this->db,$_POST['lang']);
			$path = $getkey->flags;
			$img = "../".UPLOAD_FLAG.$path;
			BgcmsMultiLanguageBean::del_lang($this->db,$getkey->name_lang,$_POST['lang']);
			if (!is_dir($img))
				unlink($img);
			$lang = array();	
			$lang['OK'] = 1;
			$lang['message'] = ("Language : ".$_POST['lang']."  has been deleted."); 
			echo array2json($lang);		
		}
		
		if(isset($_GET['act']) && $_GET['act']== 'edit_lang')
		{
				$lang = BgcmsMultiLanguageBean::get_lang_byID($this->db,$_GET['id']);
				if($lang->flags!="")
					$lang->img='<img src="../'.UPLOAD_FLAG.$lang->flags.'"/>';
				$this->smarty->assign("GETLANG",$lang);
		}
		if(isset($_GET['message']) && $_GET['message']!="")
		{
			$getlang = BgcmsMultiLanguageBean::get_lang_byID($_GET['message']);
			$mess = "Language ".$getlang['name_lang']." has been updated !!!";
			$this->smarty->assign("message",$mess);
		}	
		$lang = BgcmsMultiLanguageBean::get_all_lang($this->db);
		if($lang!=""){
			foreach($lang as $item){
				if($item->flags!="")
					$item->img='<img src="../'.UPLOAD_FLAG.$item->flags.'"/>';
			}
		}	
		$this->smarty->assign("ALL_LANGUAGE",$lang);	
	}
	
	public function create_language() {

		if(isset($_POST['key_lang']))
			$key_lang	= $_POST['key_lang'];
		if(isset($_POST['name_lang']) && $_POST['name_lang']!="")
			$name_lang 	 = $_POST['name_lang'];
		if(isset($_POST['active']))
			$active = $_POST['active'];
		
		$message = $this->validate_language($key_lang,$name_lang,$_FILES['flag']['name'],"","new");
		
		if($message != "") {				
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=lang&act=new_lang");		
		} else {
		
			if($_FILES['flag']['name'] != null && $_FILES['flag']['name'] != "") {
				$upload = new BgcmsUpload();
				$image = $_FILES['flag']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_file_small($_FILES['flag']['tmp_name'], "flag", $image_type,UPLOAD_FLAG);
				} 
			}
			$lang = array (
						'key_lang' => $key_lang,
						'flag'=>$imagename,
						'name_lang' => $name_lang,
						'active' => $active
					);	
			BgcmsMultiLanguageBean::save_lang($this->db,$lang);
			$message = "Language has been added";
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=lang");
		}
		
	}
	
	
	public function update_language() {

		if(isset($_POST['lang_id']))
			$lang_id	= $_POST['lang_id'];
		if(isset($_POST['key_lang']))
			$key_lang	= $_POST['key_lang'];
		if(isset($_POST['name_lang']) && $_POST['name_lang']!="")
			$name_lang 	 = $_POST['name_lang'];
		if(isset($_POST['active']))
			$active = $_POST['active'];
		$lang_one = BgcmsMultiLanguageBean::get_lang_byID($this->db,$lang_id);
		$message = $this->validate_language($key_lang,$name_lang,$_FILES['flag']['name'],$lang_id,'update');
		
		if($message != "") {				
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=lang&act=edit_lang&id=".$lang_id);		
		} else {
		
			if($_FILES['flag']['name'] != null && $_FILES['flag']['name'] != "") {
				$upload = new BgcmsUpload();
				$image = $_FILES['flag']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_file_small($_FILES['flag']['tmp_name'], "flag", $image_type,UPLOAD_FLAG);
				} 
				@unlink("../".UPLOAD_FLAG.$lang_one->flags);
			}else{
				$imagename = $lang_one->flags;
			}
			$lang = array (
						'lang_id' => $lang_id,
						'key_lang' => $key_lang,
						'flag'=>$imagename,
						'name_lang' => $name_lang,
						'active' => $active
					);
			$name_check = 0;			
			if($lang_one->name_lang!=$lang['name_lang'])
				$name_check ='1';		
			BgcmsMultiLanguageBean::update_lang($this->db,$lang,$lang_one->name_lang,$name_check);
			$message = "Language has been updated";
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=lang");
		}
		
	}
	
	public function translator() 
	{
		$alllang = BgcmsMultiLanguageBean::get_all_lang($this->db);	
		if(isset($_POST['act']) && $_POST['act']== 'updatemultilanguage')
		{
			$this->update_multilanguage();	
		}	
    	else if(isset($_POST['act']) && $_POST['act']== 'addmultilanguage')
		{
			$this->save_multilanguage('0');	
		}
		else if(isset($_POST['act']) && $_POST['act']== 'removelang')
		{
			BgcmsMultiLanguageBean::del_translator_lang($this->db,$_POST['lang']);
			$lang = array();	
			$lang['OK'] = 1;
			$lang['message'] = ("MultiLangugage : ".$_POST['lang']."  has been deleted."); 
			echo array2json($lang);		
		}
		if(isset($_GET['act']) && $_GET['act']== 'edit_trans')
		{
				$lang = BgcmsMultiLanguageBean::view_translator_bykey($this->db,$_GET['id']);
				$this->smarty->assign("MULTILANG",$lang);
				
		}
		if(isset($_POST['sort']) && $_POST['sort']== 'true')
		{
			$sort = $_POST['sorttype'];
			$_SESSION['sort'] = $sort; 
		} else {$sort ="";}
		if(isset($_POST['act']) && $_POST['act']== 'search')
		{
			if($_POST['keysearch']!="")
			{
				$strkey = '';
				foreach($alllang as $item)
				{
					$name = $item->name_lang;
					$lang[$name] = isset($_POST[$name]);
					$strkey .= " OR ".$name." like '%%".$_POST['keysearch']."%%'";
				}
				if($_POST['typesearch']=="all")
					$search = "AND (language_const_id like '%%".$_POST['keysearch']."%%' ".$strkey.")";	
				else
					$search = "AND (".$_POST['typesearch']." like '%%".$_POST['keysearch']."%%')";	
			}
		} else {$search ="";}
		
		$this->smarty->assign("ALL_LANGUAGE",$alllang);
		$this->view_multilanguage('0',$sort,$search);
	}
	
	
	public function save_multilanguage($for_email) 
	{
		$lang = array();
		$lang['multilanguage'] = $_POST['multilanguage'];
		$alllang = BgcmsMultiLanguageBean::get_all_lang($this->db);	
		foreach($alllang as $item)
		{
			$name = $item->name_lang;
			$lang[$name] = $_POST[$name];
		}
		$lang['for_email'] = $for_email;	
		$getkey = BgcmsMultiLanguageBean::view_translator_bykey($this->db,$_POST['multilanguage']);
		if($getkey!="")
		{
			$message = ("This MultiLangugage is exixst."); 	
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=translator&act=new_trans");
		}
		else if($lang['multilanguage']=="")
		{
			$message = ("You have to fill out all fields !!!"); 
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=translator&act=new_trans");	
		}
		else
		{
			BgcmsMultiLanguageBean::save_translator_lang($this->db,$lang);
			if($for_email!='1')
				$message = ("New MultiLangugage has been added."); 
			else
				$message = ("New Email MultiLangugage has been added.");
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsMultiLanguage&sub=translator");	
					
		}
			
    }
	
	
	function view_multilanguage($for_email,$sort,$search) 
	{
		$arr_lang = BgcmsMultiLanguageBean::view_lang($this->db,$for_email,$sort,$search);	
		//$arr_lang_paging = BgcmsMultiLanguageBean::view_lang_paging($this->db,ADMINPAGING,'list',$for_email,$sort,$search);
		$arr_lang_paging = BgcmsMultiLanguageBean::view_lang_paging($this->db,300,'list',$for_email,$sort,$search);
		$field = array(); 
		$result = array(); 
		$field['data'] = $arr_lang_paging;
		if (isset($_GET['module'])) {
			//$result = BgcmsPaging::do_paging($arr_lang,$field, ADMINPAGING, "?module=".$_GET['module']."&sub="."translator","list");
			$result = BgcmsPaging::do_paging($arr_lang,$field, 300, "?module=".$_GET['module']."&sub="."translator","list");
		}
		$this->smarty->assign("LISTLANG",$result);
			
	}
	
	function update_multilanguage() 
	{
			$arr = array();
			
			//$lang = BgcmsMultiLanguageBean::view_translator_bykey($_GET['id']);
			
				$lang['ID'] = "1";	
				$message = "MultiLangugage has been updated";	
				$lang['multilanguage'] = $_POST['multilanguage'];
				 	
				$alllang = BgcmsMultiLanguageBean::get_all_lang($this->db);	
				foreach($alllang as $item)
				{
					$name = $item->name_lang;
					$lang[$name] = $_POST[$name];
				}
				BgcmsMultiLanguageBean::update_translator($this->db,$lang);
				$lang['msg'] = $lang['multilanguage'];
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsMultiLanguage&sub=translator");
	}
	
	public function validate_language($key,$name,$flag,$lang_id,$type){
		$getkey = BgcmsMultiLanguageBean::get_lang_bykey($this->db,$key);
		$getname = BgcmsMultiLanguageBean::get_lang_byname($this->db,$name);
		if($key == "")
			$msg = "This Key Language could not be empty .";
			
		if(($lang_id=="" && $getkey != "") || ($lang_id!="" && $getkey != "" && $getkey->lang_id != $lang_id))
			$msg .= ($msg==""?"":"<br />") . "This Key Language is exixst .";
		if(($lang_id=="" && $getname != "") || ($lang_id!="" && $getname != "" && $getname->lang_id != $lang_id))
			$msg .= ($msg==""?"":"<br />") . "This Name Language is exixst .";		
		if($name == "")	
			$msg .= ($msg==""?"":"<br />") . 
						 "This Name Language could not be empty .";
		if($flag == "" && $type !="update")	
			$msg .= ($msg==""?"":"<br />") . 
						 "This Name Language could not be empty .";				 
		return $msg;				 
	}
	
}
?>